package leetcode_2401_2500;

import java.util.Arrays;

public class LeeCode_2404 {
    public static void main(String[] args) {
//        System.out.println(mostFrequentEven(new int[]{0, 1, 2, 2, 4, 1}));
        System.out.println(mostFrequentEven(new int[]{4,4,4,9,2,4}));
    }
    private static int mostFrequentEven(int[] nums) {
        Arrays.sort(nums);
        int n = nums.length;
        int i = 0;
        int pre = -1;
        int max = 0;
        int ans = 0;
        while (i < n){
            while (i < n && nums[i] % 2 != 0){
                i++;
            }
            int count = -1;
            if (i < n) {
                pre = nums[i];
                count = 0;
            }
            while (i < n && nums[i] == pre){
                i++;
                count++;
            }
            if (count != -1) {
                if (count > max){
                    ans = nums[i - 1];
                    max = count;
                }
            }
        }
        return max == 0 ? -1 : ans;
    }
}
